import prompt from '@system.prompt';
import router from '@system.router';

var timer // 定时器
var myindex // 用于传参

export default {
    data: {
        todoList: [
            {
                status: '', // true或false
                time: '07:00',
                medicine: '阿司匹林',
                dosage: '2',
                overtime: '', // 超时或还有
//                timeout: '3小时22分',
                timeout: '', // 超时或还有多长时间
//                parameter : 0, // 传参
            },
            {
                status: '',
                time: '09:50',
                medicine: '维生素C',
                dosage: '2.5',
                overtime: '',
//                timeout: '2小时22分',
                timeout: '',
//                parameter: 1,
            },
            {
                status: '',
                time: '19:00',
                medicine: '阿司匹林',
                dosage: '2',
                overtime: '',
//                timeout: '3小时22分',
                timeout: '',
//                parameter: 2,
            },
            {
                status: '',
                time: '21:50',
                medicine: '维生素C',
                dosage: '2.5',
                overtime: '',
//                timeout: '1小时22分',
                timeout: '',
//                parameter: 3,
            },
        ],
        todoCount: '',
        pickervalue: '点击选择时间',
        containsecond: false,
        textcolor: 'gray',
//        medicineinput: '板蓝根',
//        dosageinput:'2',
        medicineinput: '',
        dosageinput:'',

        showpickervalue: '',
        showmedicineinput: '',
        showdosageinput: '',
    },

    onShow() {
        timer = setInterval(this.changestate, 1000) // 每个1000毫秒执行 drawPie 函数
        console.info('使用定时器')
    },
    onHide() {
        clearInterval(timer)
        console.info('停止定时器')
    },

    changestate() {
        var nowtime = new Date(); //获取系统当前时间（此处获取的是GMT时间，格林尼治标准时间，是指世界时间，即0时区的区时，比北京时间（东8区）晚8小时）
        //  console.info(nowtime)
        //  console.info(nowtime.getHours()+8)
        //  console.info(nowtime.getUTCHours())
        var timezone = 8; //目标时区时间，东八区
        var offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差，单位为分钟
        var nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜（GMT 时间）之间的毫秒数

//        console.info(nowDate)
        var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
//        console.log("东8区现在是：" + targetDate.getHours());
//        console.info(nowtime.getMinutes())
        var nowhour = targetDate.getHours()
        var nowminute = nowtime.getMinutes()
        this.todoList.forEach(todolist => {
            var thetime = todolist.time.split(':')
//            console.info(thetime)
//            console.info(Number(thetime[0]))
            if(nowhour >= Number(thetime[0]) && nowminute >= Number(thetime[1])) {
                todolist.status = false
                todolist.overtime = '超时'
                todolist.timeout = String(nowhour - Number(thetime[0])) + '小时' + String(nowminute - Number(thetime[1])) + '分'
            }
            if(nowhour > Number(thetime[0]) && nowminute < Number(thetime[1])) {
                todolist.status = false
                todolist.overtime = '超时'
                todolist.timeout = String(nowhour - Number(thetime[0]) - 1) + '小时' + String(60 - Number(thetime[1]) + nowminute) + '分'
//                console.info(todolist.timeout)
            }
            if(nowhour < Number(thetime[0]) && nowminute > Number(thetime[1])) {
                todolist.status = true
                todolist.overtime = '还有'
                todolist.timeout = String(Number(thetime[0]) - nowhour - 1) + '小时' + String(60 + Number(thetime[1]) - nowminute) + '分'
            }
            if(nowhour <= Number(thetime[0]) && nowminute <= Number(thetime[1])) {
                todolist.status = true
                todolist.overtime = '还有'
                todolist.timeout = String(Number(thetime[0]) - nowhour) + '小时' + String(Number(thetime[1]) - nowminute) + '分'
//                console.info(todolist.timeout)
            }
        })
        this.todoCount = 0;
        this.todoList.forEach(element => { // forEach方法对数组的每个元素调用定义的回调函数，并返回包含结果的数组
            if (element.status) {
                this.todoCount = this.todoCount + 1
            }
        });
    },

    medicineinputchange(e) {
        this.medicineinput = e.value; // 若输入框中内容变化，将动态获取到的 value 中的值赋给 medicineinput
    },
    dosageinputchange(e) {
        this.dosageinput = e.value; // 若输入框中内容变化，将动态获取到的 value 中的值赋给 dosageinput
//        console.info('获取：'+e.value)
//        console.info('获取：'+this.dosageinput)
    },
    /**
     * 虚拟机调试打开以下注释，以动态获取输入框值
     * */
    medicineinputchangeagain(e) {
        this.showmedicineinput = e.value; // 若输入框中内容变化，将动态获取到的 value 中的值赋给 showmedicineinput
    },
    dosageinputchangeagain(e) {
        this.showdosageinput = e.value; // 若输入框中内容变化，将动态获取到的 value 中的值赋给 showdosageinput
        console.info('变化：'+this.showdosageinput)
    },
    timeonchangeagain(e) {
        if(this.containsecond) {
            this.textcolor='black';
            if (e.hour < 10 && e.minute < 10 && e.second < 10) {
                this.showpickervalue='0' + e.hour+":"+'0'+e.minute+":"+'0'+e.second;
            }
            if (e.hour < 10 && e.minute < 10 && e.second > 10) {
                this.showpickervalue='0' + e.hour+":"+'0'+e.minute+":"+e.second;
            }
            if (e.hour < 10 && e.minute > 10 && e.second > 10) {
                this.showpickervalue='0' + e.hour+":"+e.minute+":"+e.second;
            }
            if (e.hour < 10 && e.minute > 10 && e.second < 10) {
                this.showpickervalue='0' + e.hour+":"+e.minute+":"+'0'+e.second;
            }
            if (e.hour > 10 && e.minute < 10 && e.second < 10) {
                this.showpickervalue=e.hour+":"+'0'+e.minute+":"+'0'+e.second;
            }
            if (e.hour > 10 && e.minute < 10 && e.second > 10) {
                this.showpickervalue= e.hour+":"+'0'+e.minute+":"+e.second;
            }
            if (e.hour > 10 && e.minute > 10 && e.second < 10) {
                this.showpickervalue=e.hour+":"+e.minute+":"+'0'+e.second;
            }
            if (e.hour >= 10 && e.minute >= 10 && e.second >= 10) {
                this.showpickervalue=e.hour+":"+e.minute+":"+e.second;
            }
            prompt.showToast({ message:"选择时间:" + this.showpickervalue })
        } else {
            if (e.hour < 10 && e.minute < 10) {
                this.showpickervalue='e.minute';
            }
            if (e.hour < 10 && e.minute > 10) {
                this.showpickervalue='0' + e.hour+":"+e.minute;
            }
            if (e.hour > 10 && e.minute < 10) {
                this.showpickervalue=e.hour+":" + '0'+e.minute;
            }
            if (e.hour >= 10 && e.minute >= 10) {
                this.showpickervalue=e.hour+":" +e.minute;
            }
            prompt.showToast({ message:"已选择时间:" + this.showpickervalue })
        }},
    remove(index) { // 删除代办事项
        console.log(index) // 打印日志
        this.todoList.splice(index, 1); // 实现删除效果，从第index个开始删除1个
//        this.todoList[index].parameter = this.todoList[index].parameter - 1
    },
    switchChange(index) { // 编辑已添加提醒
        myindex = index
        console.info('点击了第'+index+'个')
        console.info(this.todoList)
        console.info(this.todoList[index])
        this.showpickervalue = this.todoList[index].time
        console.info(this.showpickervalue)
        this.showmedicineinput = this.todoList[index].medicine
        console.info(this.showmedicineinput)
        this.showdosageinput = this.todoList[index].dosage
        console.info(this.todoList[index].dosage)
        this.$element('changeDialog').show() // 弹出对话框
        console.info('弹出')
    },
    cancelClickChange() {
        this.$element('changeDialog').close() // 关闭对话框
        prompt.showToast({message: '已取消'})
    },
    sureClickChange() {
//        this.parameter = this.todoList.length
//        console.info(e.target.dataSet.par)
//        console.info(this.pickervalue)
        if (this.showpickervalue == "点击选择时间") {
            prompt.showToast({message: '请选择时间'})
        }
        else if (this.showmedicineinput == "") {
            prompt.showToast({message: '请选择药物名称'})
        }
        else if (this.showdosageinput == "") {
            prompt.showToast({message: '请选择药物剂量'})
        }
        else if(this.showpickervalue != '点击选择时间' && this.showmedicineinput != '' && this.showdosageinput != '') {
            var changeshow = {
                status: '',
                time: this.showpickervalue,
                medicine: this.showmedicineinput,
                dosage: this.showdosageinput,
                overtime: '',
                timeout: '',
            }

//            this.todoList.splice(index, 1)
//            this.todoList.splice(index, 0 ,changeshow)
            this.todoList.splice(myindex, 1, changeshow)

//            console.info(this.showmedicineinput)
//            console.info(changeshow.medicine)

            this.$element('changeDialog').close() // 关闭对话框
//            prompt.showToast({message: '已修改'})
        }
    },

//    addTodo() { // 添加代办
//        this.todoList.push({ // push方法组合两个或多个数组
//            info: 'IDE工具无法监听键盘输入',
//            status: false
//        })
//        this.setStorage();
//    },
    showeventDialog() {
        this.$element('eventDialog').show() // 弹出对话框
    },
    cancelClick() {
        this.$element('eventDialog').close() // 关闭对话框
        prompt.showToast({message: '已取消'})
    },
    sureClick() {
//        console.info(this.pickervalue)
        if (this.pickervalue == "点击选择时间") {
            prompt.showToast({message: '请选择时间'})
        }
        else if (this.medicineinput == "") {
            prompt.showToast({message: '请选择药物名称'})
        }
        else if (this.dosageinput == "") {
            prompt.showToast({message: '请选择药物剂量'})
        }
        else if(this.pickervalue != '点击选择时间' && this.medicineinput != '' && this.dosageinput != '') {

            this.todoList.push({
                time: this.pickervalue,
                medicine: this.medicineinput,
                dosage: this.dosageinput,
                overtime: '',
                timeout: '',
                status: '',
//                parameter: this.todoList.length - 1
            })

            this.$element('eventDialog').close() // 关闭对话框
            prompt.showToast({message: '已添加'})
        }
    },
//    computed: { // 更新要吃药的次数
//        todoCount() {
//            let num = 0;
//            this.todoList.forEach(element => { // forEach方法对数组的每个元素调用定义的回调函数，并返回包含结果的数组
//                if (!element.status) {
//                    num++
//                }
//            });
//            return num
//        }
//    },
    timeonchange(e) {
        if(this.containsecond){
            this.textcolor='black';
//            this.pickervalue=e.hour+":"+e.minute+":"+e.second;
//            prompt.showToast({ message:"选择时间:" + e.hour + ":" + e.minute + ":" + e.second })
            if (e.hour < 10 && e.minute < 10 && e.second < 10) {
                this.pickervalue='0' + e.hour+":"+'0'+e.minute+":"+'0'+e.second;
            }
            if (e.hour < 10 && e.minute < 10 && e.second > 10) {
                this.pickervalue='0' + e.hour+":"+'0'+e.minute+":"+e.second;
            }
            if (e.hour < 10 && e.minute > 10 && e.second > 10) {
                this.pickervalue='0' + e.hour+":"+e.minute+":"+e.second;
            }
            if (e.hour < 10 && e.minute > 10 && e.second < 10) {
                this.pickervalue='0' + e.hour+":"+e.minute+":"+'0'+e.second;
            }
            if (e.hour > 10 && e.minute < 10 && e.second < 10) {
                this.pickervalue=e.hour+":"+'0'+e.minute+":"+'0'+e.second;
            }
            if (e.hour > 10 && e.minute < 10 && e.second > 10) {
                this.pickervalue= e.hour+":"+'0'+e.minute+":"+e.second;
            }
            if (e.hour > 10 && e.minute > 10 && e.second < 10) {
                this.pickervalue=e.hour+":"+e.minute+":"+'0'+e.second;
            }
            if (e.hour > 10 && e.minute > 10 && e.second > 10) {
                this.pickervalue=e.hour+":"+e.minute+":"+e.second;
            }
            prompt.showToast({ message:"选择时间:" + this.pickervalue })
        } else {
            this.textcolor='black';
//            this.pickervalue=e.hour+":"+e.minute;
//            prompt.showToast({ message:"Time:" + e.hour + ":" + e.minute })
            if (e.hour < 10 && e.minute < 10) {
                this.pickervalue='e.minute';
            }
            if (e.hour < 10 && e.minute > 10) {
                this.pickervalue='0' + e.hour+":"+e.minute;
            }
            if (e.hour > 10 && e.minute < 10) {
                this.pickervalue=e.hour+":" + '0'+e.minute;
            }
            if (e.hour > 10 && e.minute > 10) {
                this.pickervalue=e.hour+":" +e.minute;
            }
            prompt.showToast({ message:"已选择时间:" + this.pickervalue })
        }},
    timeoncancel() {
        prompt.showToast({ message:"已取消" })
    },
    ToBackManage() {
        router.back()
    },
}
